package com.nbgroup.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.toolkit.Db;
import com.nbgroup.pojo.Business;
import com.nbgroup.mapper.BusinessMapper;
import com.nbgroup.pojo.User;
import com.nbgroup.pojo.common.PageBean;
import com.nbgroup.pojo.dto.AssignmentOrGainBusiDTO;
import com.nbgroup.pojo.dto.BusinessPageQueryDTO;
import com.nbgroup.service.IBusinessService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * <p>
 * 商机表 服务实现类
 * </p>
 *
 * @author author
 * @since 2024-10-27
 */
@Service
public class BusinessServiceImpl extends ServiceImpl<BusinessMapper, Business> implements IBusinessService {


    /**
     * 商机分页查询
     * @param dto
     * @return
     */
    @Override
    public PageBean PageQuery(BusinessPageQueryDTO dto) {
        dto.check();
        Map<String, Object> params = dto.getParams();
        Page<Business> page = new Page<>(dto.getPageNum(),dto.getPageSize());
        Page<Business> p = lambdaQuery().eq(dto.getId() != null, Business::getId, dto.getId())
                .like(dto.getPhone() != null && dto.getPhone() != "", Business::getPhone, dto.getPhone())
                .like(dto.getOwner() != null && dto.getOwner() != "", Business::getOwner, dto.getOwner())
                .like(dto.getName() != null && dto.getName() != "", Business::getName, dto.getName())
                .eq(dto.getStatus() != null, Business::getStatus, dto.getStatus())
                .ge(params.get("beginCreateTime") != "", Business::getCreateTime, params.get("beginCreateTime"))
                .le(params.get("endCreateTime") != "", Business::getCreateTime, params.get("endCreateTime"))
                .orderBy(true,false,Business::getCreateTime)
                .page(page);
        PageBean pageBean = PageBean.builder().rows(p.getRecords()).total(p.getTotal()).msg("查询成功").code(200)
                .build();
        return pageBean;
    }

    /**
     * 分配商机
     * @param dto
     */
    @Override
    public void gainOrAssignBusi(AssignmentOrGainBusiDTO dto) {
        lambdaUpdate().in(Business::getId,dto.getIds()).set(Business::getUserId,dto.getUserId()).update();
    }
}
